/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client;

import nu.zoom.android.gdata.project.client.activity.CommentsList;
import nu.zoom.android.gdata.project.client.activity.IssuesList;
import nu.zoom.android.gdata.project.client.activity.ManageProject;
import nu.zoom.android.gdata.project.client.activity.SettingsActivity;
import nu.zoom.android.gdata.project.client.activity.ViewIssue;
import nu.zoom.android.gdata.project.integration.ProjectHostingClient;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public final class Intents {

	public static final class RequestCode {
		public static final int ADD_PROJECT = 1;
		public static final int EDIT_PROJECT = 2;
	}

	public static final class Extra {
		public static final String PROJECT_NAME = "projectname";
		public static final String PROJECT_INDEX = "projectindex";
		public static final String ISSUE_NAME = "commentname";
		public static final String ISSUE_INDEX = "commentname";
	}

	private Intents() {
		// This class shouldn't be instantiated
	}

	public static Intent viewProject(final Context context,
			final String projectName) {
		Intent intent = new Intent(context, IssuesList.class);
		intent.setAction(Intent.ACTION_VIEW);
		Uri uri = Uri.parse(ProjectHostingClient.FEED_PREFIX + projectName
				+ ProjectHostingClient.FEED_SUFFIX);
		intent.setData(uri);
		intent.putExtra(Extra.PROJECT_NAME, projectName);
		return intent;
	}

	public static Intent addProject(final Context context) {
		Intent intent = new Intent(context, ManageProject.class);
		intent.setAction(Intent.ACTION_INSERT);
		return intent;
	}

	public static Intent editProject(final Context context,
			final String projectName, final int projectIndex) {
		Intent intent = new Intent(context, ManageProject.class);
		intent.setAction(Intent.ACTION_EDIT);
		intent.putExtra(Extra.PROJECT_NAME, projectName);
		intent.putExtra(Extra.PROJECT_INDEX, projectIndex);
		return intent;
	}

	public static Intent viewIssue(final Context context, final String issueID,
			final String projectName) {
		Intent intent = new Intent(context, ViewIssue.class);
		intent.setAction(Intent.ACTION_VIEW);
		Uri uri = Uri.parse(issueID);
		intent.setData(uri);
		if (projectName != null) {
			intent.putExtra(Extra.PROJECT_NAME, projectName);
		}
		return intent;
	}

	public static Intent viewProjectWeb(final Uri feedUri) {
		String urlString = feedUri.toString();
		urlString = urlString.replaceAll(ProjectHostingClient.FEED_PREFIX,
				ProjectHostingClient.WEB_ISSUELIST_PREFIX);
		urlString = urlString.replaceAll(ProjectHostingClient.FEED_SUFFIX,
				ProjectHostingClient.WEB_ISSUELIST_SUFFIX);
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(urlString));
		return intent;
	}

	public static Intent viewIssueWeb(final Uri webUri) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setData(webUri);
		return intent;
	}

	/**
	 * @param data
	 * @return
	 */
	public static Intent viewIssueComments(final Context context,
			final Uri feedUri, final String issueTitle) {
		Intent intent = new Intent(context, CommentsList.class);
		intent.setAction(Intent.ACTION_VIEW);
		String issueID = feedUri.getLastPathSegment();
		String urlString = feedUri.toString();
		urlString = urlString.replaceAll("full/" + issueID, issueID
				+ "/comments/full");
		intent.setData(Uri.parse(urlString));
		if (issueTitle != null) {
			intent.putExtra(Extra.ISSUE_NAME, issueTitle);
		}
		return intent;
	}

	/**
	 * @return
	 */
	public static Intent settings(final Context context) {
		Intent intent = new Intent(context, SettingsActivity.class);
		return intent;
	}
}
